package main

import (
	"fmt"
	"strings"
)

// import "reflect"

func main() {
	//fmt.Println("str := \"c:\\pprof\\main.exe\"")

	/*
		s := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
		// l := s[2:5]
		// l := s[:5]
		l := s[2:]
		fmt.Println("slice1:", l)

		l[0] += 100
		l[1] += 200
		fmt.Println("sliceNew:", l)

		// hello.Print()
		buf := make([]byte, 1024)
		f, _ := os.Open("/Users/shukang/Desktop/登录对接.txt")
		defer f.Close()
		for {
			n, _ := f.Read(buf)
			if n == 0 {
				break
			}
			os.Stdout.Write(buf[:n])
		}

		others := "              d"
		flag := "d"
		fmt.Println("length", len(others))
	*/

	//删除s尾部连续的包含在str中的字符串
	/*
		strings.TrimRight其实就是把cutset里面的字符串拆分成字符，
		然后从右往左，逐个对比字符中的每个字符，直到遇到没有在cutset中出现的字符
	*/

	others := "aabbccdd"
	flag := "abcd"

	others = strings.TrimRight(others, flag)
	fmt.Println("处理后：", others, "trimRight长度为：", len(others))
	//fmt.Println("aabbccdd\t:", strings.TrimRight("aabbccdd", "abcd")) // 空字符串

	/*
		cMap := make(map[string]int)
		var wg sync.WaitGroup
		wg.Add(2)
		go func() {
			cMap["北京"] = 1
			wg.Done()
		}()
		go func() {
			cMap["上海"] = 2
			wg.Done()
		}()
		wg.Wait()

	*/

	//var s = []string{"11","22","33"}

	/*
		testStr := "fas dhh ges.t r.rstr in g.s tri ng *"
		testStr = strings.TrimRight(testStr, " ")
		fmt.Println("TrimRight a:", testStr) //这样的输出结果是l
	*/
	/*
		a := "le23x.e.exe"
		a = strings.TrimRight(a, ".exe")
		fmt.Println("TrimRight a:", a) //这样的输出结果是l

		strHaiCoder := "HaiCoder 嗨客网1 2HaiCoder"
		TrimRightStr := strings.TrimRight(strHaiCoder, "HaiCoder")
		fmt.Println("TrimRightStr =", TrimRightStr)
	*/

	/*
	   	s1 := `第一行
	   第二行
	   第三行`
	       fmt.Println(s1)

	   	a := sort.IntSlice{2, 8, 3, 7, 9, 4, 1, 6,5}
	   	fmt.Println("排序前", a)
	   	// sort.Sort(a) //升序
	   	sort.Sort(sort.Reverse(a)) //这是降序
	   	fmt.Println("排序后", a)

	   	// sum := caculator(10,20)
	   	// fmt.Println("sum:= ", sum)
	   	// sumAction(0,3)


	   	s := []int{1, 2, 3}
	       res := test("sum: %d", s...)    // slice... 展开slice
	       println(res)
	*/

	/*
		sliceA := []int{1,2,3,4,5}
		sum := addAction(sliceA)//这里是错误写法
		fmt.Println("sum:= ", sum)
	*/

	/*
		sum :=addAction(5,10)
		fmt.Println("sum:= ", sum)
	*/

	/*
			// rand.Seed(time.Now().Unix())
			rand.Seed(time.Now().UnixNano())//初始化随机数种子

			var scoreMap = make(map[string] int,200)
			for i := 0; i < 100; i++ {
				key := fmt.Sprintf("stu %02d",i)
				value := rand.Intn(100)
				scoreMap[key] = value
			}

			//对map进行range遍历，得到的顺序是杂乱的
			for key, value := range scoreMap {
				fmt.Println("key:=",key, "value:=",value)
			}

			var keys = make ([]string, 0,200)
			for key := range scoreMap {
				keys = append(keys,key)
			}

			sort.Strings(keys)
			//按照排序后的key遍历map
		    for _, key := range keys {
		        fmt.Println(key, scoreMap[key])
		    }
	*/

	/*
		valueA := 10
		valueB := 0
		valueB = valueA
		fmt.Println("1————valuea:%d valueb:%d",valueA,valueB)
		valueB = 20
		fmt.Println("2————valuea:%d valueb:%d",valueA,valueB)

		sliceA := []int{0,1,2,3}
		sliceB := []int{}
		sliceB = sliceA
		fmt.Println("1————sliceA:",sliceA ,"sliceB:",sliceB)
		sliceB = append(sliceB,99)
		// sliceA = append(sliceA,99)
		fmt.Println("2————sliceA:",sliceA ,"sliceB:",sliceB)
	*/

	/*
		rand.Seed(1)
		x := rand.Intn(100)
		fmt.Println("x:",x)
	*/

	/*
			//这里主要切片和数组的简写
			s := [...]int{0,1,2,3,4,5,6,7,8,9}
			// s = append(s,100)
			// fmt.Println("打印s:",s[:])
			// fmt.Println("capacity:",cap(s))

			sum := sumArr(s)
			fmt.Println("summary:",sum)

			c := [5]int{2: 100, 4: 200}
			fmt.Println("打印c:",c)

			a := [3]int{}
		    println(len(a), cap(a))
	*/

	/*
		myMap := map[string]string{
			"key1":"red",
			"key2":"yellow",
			"key3":"orange",
		}
		delete(myMap,"key1")
		fmt.Println(myMap)
	*/

	/*
			//指针
		 	a:= 10
			fmt.Println(&a)

			p:= &a
			fmt.Println(p)
			fmt.Println("type:",reflect.TypeOf(p))

			b := "str"
			var p1 *string = &b
			fmt.Println(p1)
	*/

	/*
		fruit := []string{"apple","banana","orange","Pear",
		"strawberry","watermelon","grape"}

		// fmt.Println(fruit[1:])
		//增加元素
		// fruit = append(fruit,"strawberry")
		// slice := fruit[:1]
		slice := fruit[2:4]
		fmt.Println(slice)

		fruit = append(fruit[:1],fruit[2:4]...)
		fmt.Println(fruit)


		//删除元素
	*/

	/*
		var(
			c = 90
			d = "hello"
		)
		fmt.Println("c:",c,"d:",d)

		const a = 10
		// a = 20  //报错：cannot assign to a (neither addressable nor a map index expression)
		fmt.Println("a:",a)

	*/

	/*
		const(
			h = iota
			i = iota
			f = 67
			g = 89
		)
		fmt.Println(f,g,h,i)
	*/

	/*
		const(
			o = iota
			p
			q
			r
			s
		)
		fmt.Println(o,p,q,r,s)
	*/
}

// 求元素和
func sumArr(a [10]int) int {
	var sum int = 0
	for i := 0; i < len(a); i++ {
		sum += a[i]
	}
	return sum
}

// 返回值,命名返回值
func caculator(oneParam int, twoParam int) (sum int) {
	sum = oneParam + twoParam
	return
}

func sumAction(n ...int) int {
	var x int
	for _, i := range n {
		x += i
	}
	fmt.Println("sumAction:= ", x)
	return x
}

func addAction(x, y int) (z int) {
	//注意return的返回简写
	/*
	   { // 不能在一个级别，引发 "z redeclared in this block" 错误。
	       var z = x + y
	       // return   // Error: z is shadowed during return
	       return z// 必须显式返回。
	   }
	*/

	z = x + y
	// return   // Error: z is shadowed during return
	return
	//这也是错误写法

}

func test(s string, n ...int) string {
	var x int
	for _, i := range n {
		x += i
	}
	return fmt.Sprintf(s, x)
}
